import java.awt.Point;

import me.w70.bot.Bot;
import me.w70.bot.methods.Calc;
import me.w70.bot.scripting.Random;
import me.w70.bot.wrappers.RSInterface;
import me.w70.bot.wrappers.RSInterfaces;
import me.w70.bot.wrappers.RSObject;
import me.w70.hooks.Tile;


public class Pillory extends Random {

	public int fail = 0;
	private final RSInterface GameInterface = RSInterfaces.getInterface(189);
	public boolean inCage = false;
	public int[] myLoc;
	private String pilloryMessage = "Solve the Pillory puzzle to be returned to where you came from.";

	public Tile South = new Tile(2606, 3105);

	@Override
	public boolean canRun() {
		if (!getInterface(372,0).isValid()) {
			return false;
		}

		if (inCage != true) {
			inCage = getInterface(372, 2).getText().contains(
			"Solve the pillory");
		}
		return inCage;
	}

	public RSObject findMYObject(final int... ids) {
		RSObject cur = null;
		int dist = -1;
		for (int x = 0; x < 104; x++) {
			for (int y = 0; y < 104; y++) {
				final RSObject o = getObjectAt(x + getClient().getBaseX(),
						y + getClient().getBaseY());
				if (o != null) {
					boolean isObject = false;
					for (final int id : ids) {
						if (o.getID() == id) {
							isObject = true;
							break;
						}
					}
					if (isObject) {
						final int distTmp = distanceTo(o.getLocation());
						if (distTmp != -1) {
							if (cur == null) {
								dist = distTmp;
								cur = o;
							} else if (distTmp < dist) {
								cur = o;
								dist = distTmp;
							}
						}
					}
				}
			}
		}
		return cur;
	}

	@Override
	public String getScriptAuthor() {
		return "spooky";
	}

	private int getKey() {
		int key = 0;
		Bot.addMessage(this, "\tKey needed :");
		switch (GameInterface.getChild(4).getModelID()) {
		case 9753:
			key = 9749;
			Bot.addMessage(this, "\t   Diamond");
			break;
		case 9754:
			key = 9750;
			Bot.addMessage(this, "\t   Square");
			break;
		case 9755:
			key = 9751;
			Bot.addMessage(this, "\t   Circle");
			break;
		case 9756:
			key = 9752;
			Bot.addMessage(this, "\t   Triangle");
			break;
		}
		if (GameInterface.getChild(5).getModelID() == key) {
			return 1;
		}
		if (GameInterface.getChild(6).getModelID() == key) {
			return 2;
		}
		if (GameInterface.getChild(7).getModelID() == key) {
			return 3;
		}
		return -1;
	}

	@Override
	public String getScriptName() {
		return "Pillory";
	}


	@Override
	public int loop() {
		setCameraAltitude(true);
		if (distanceTo(South) <= 10) {
			setCameraAngle(180);
		} else {
			setCameraAngle(360);
		}
		if (fail > 20) {
			//superfail
		}
		if (myLoc == null) {
			Bot.addMessage(this, "getting Location..");
			myLoc = new int[] { getMyPlayer().getLocation().getX(),
					getMyPlayer().getLocation().getY() };
			Bot.addMessage(this, myLoc[0] + "," + myLoc[1]);
			return random(1000, 2000);
		}
		if (getMyPlayer().getLocation().getX() != myLoc[0]
		                                                || getMyPlayer().getLocation().getY() != myLoc[1]) {
			Bot.addMessage(this, getMyPlayer().getLocation().toString());
			Bot.addMessage(this, "Solved It..");
			pilloryMessage = null;
			inCage = false;
			return -1;
		}
		if (!RSInterfaces.getInterface(189).isValid()) {
			final Point ObjectPoint = new Point(Calc
					.tileToScreen(new Tile(myLoc[0], myLoc[1])));
			final Point Lock = new Point((int) ObjectPoint.getX() + 10,
					(int) ObjectPoint.getY() - 30);
			clickMouse(Lock.x, Lock.y + random(0, 15), false);
			wait(random(600, 800));
			if (atMenu("unlock")) {
				Bot.addMessage(this, "Successfully opened the lock!");
				return random(1000, 2000);
			} else {
				fail++;
			}
		}
		if (RSInterfaces.getInterface(189).isValid()) {
			final int key = getKey();
			Bot.addMessage(this, String.valueOf(key));
			switch (key) {
			case 1:
				atInterface(GameInterface.getChild(5));
				break;
			case 2:
				atInterface(GameInterface.getChild(6));
				break;
			case 3:
				atInterface(GameInterface.getChild(7));
				break;
			default:
				Bot.addMessage(this, "Bad Combo?");
			fail++;
			break;
			}
			return random(1000, 2000);
		}
		return -1;
	}


	



	@Override
	public String getScriptCategory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getScriptDescription() {
		// TODO Auto-generated method stub
		return null;
	}


}
